# Create a descriptive plot of average positive and negative emotions and reaction choices (Figure 1).




#############
# load data #
#############

library(dplyr)
library(stargazer)
library(ggplot2)

load("PSRM Replication Files/SurveyResponses.Rdata") # data, 2008, 44
colnames(data)

# check
sum(data$dem) # 1000
sum(data$pop) # 1029




#################################
# average emotions by reactions #
#################################

# emotions
positive <- "interested|inspired|enthusiastic|proud|excited"
negative <- "upset|scared|ashamed|nervous|hostile"

# pre-treatment affect scores
data$pre_pa <- data %>% select(matches("_pre")) %>% select(matches(positive)) %>% 
  mutate(across(, as.numeric)) %>% rowSums()
data$pre_na <- data %>% select(matches("_pre")) %>% select(matches(negative)) %>% 
  mutate(across(, as.numeric)) %>% rowSums()

# post-treatment affect scores
data$post_pa <- data %>% select(matches("_post")) %>% select(matches(positive)) %>% 
  mutate(across(, as.numeric)) %>% rowSums()
data$post_na <- data %>% select(matches("_post")) %>% select(matches(negative)) %>% 
  mutate(across(, as.numeric)) %>% rowSums()

# post-pre difference in affect
data <- data %>% 
  mutate(diff_pa = post_pa - pre_pa, 
         diff_na = post_na - pre_na)

# check
dim(data) # 2008 50

# check for straight-lining PANAS
panas <- data[, c(12:21, 31:40)] %>% replace(is.na(.), "")
stline <- (rowMeans(panas[, 1:10] == panas[, 1]) == 1 | rowMeans(panas[, 11:20] == panas[, 11]) == 1)

# check
sum(stline) # 167

# dataframe: average affect scores by reaction choice
reaction_plot_data <- data %>%
  filter(!stline) %>%
  filter(!is.na(reaction_choice)) %>%
  group_by(reaction_choice) %>%
  summarise(pos_mean = mean(post_pa, na.rm=T),
            neg_mean = mean(post_na, na.rm=T),
            pos_ci = sd(post_pa, na.rm=T)/sqrt(n())*qt(0.95/2 + 0.5, n()-1),
            neg_ci = sd(post_na, na.rm=T)/sqrt(n())*qt(0.95/2 + 0.5, n()-1))

# figure 1
ggplot(data=reaction_plot_data, mapping = aes(x=reaction_choice, y=neg_mean)) + 
  geom_point(aes(color="Negative", shape="Negative"), size=2) + 
  geom_errorbar(aes(ymin=neg_mean-neg_ci, ymax=neg_mean+neg_ci, color="Negative"), width=.2) + ylim(5, 25) +
  geom_point(aes(x=reaction_choice, y=pos_mean, color="Positive", shape="Positive"), size=2) +
  geom_errorbar(aes(ymin=pos_mean-pos_ci, ymax=pos_mean+pos_ci, color="Positive"), width=.2) + ylim(5, 25) +
  ylab("Mean (PANAS)") + xlab("Reaction Choice") +
  scale_x_discrete(labels=c("Like", "Love", "Care", "Haha", "Wow", "Sad", "Angry")) +
  theme_bw() + 
  theme(axis.title=element_text(size=15), legend.title = element_text(size=15), legend.text=element_text(size=15), axis.text=element_text(size=15)) +
  scale_color_manual(name="Emotions", breaks=c("Positive", "Negative"), values=c("Positive" = "red", "Negative"="black")) +
  scale_shape_manual(name="Emotions", breaks=c("Positive", "Negative"), values=c("Positive" = 19, "Negative"=17))

#ggsave("panasreaction.pdf", plot=last_plot(), width = 7, height=4.5)

